package ChatSystem.ClientDemo.frame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 设置端口号窗体
 * @create 2022.5.29
 * @author Administrator
 *
 */
public class ClientSetPost extends JFrame implements ActionListener{

	private static final long serialVersionUID = 1L;

	//确定按钮
	private JButton set = new JButton("进入");
	
	//关闭窗口按钮
	private JButton exit = new JButton("关闭");
	
	//窗口输入
	private JTextField write = new JTextField();
	private JTextField name = new JTextField();
	
	//显示端口，姓名
	private JLabel showPost = new JLabel("端口号");
	private JLabel showName = new JLabel("姓 名");
	
	public ClientSetPost() {
		//初始化
		init();
	}
	
	private void init(){
		
		//设置窗口名称
		this.setTitle("客户端");
		
		//窗口大小固定
		this.setResizable(false);
		
		//设置窗口关闭
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//设置字体
		exit.setFont(new Font("宋体",1,18));
		set.setFont(new Font("宋体",1,18));
		write.setFont(new Font("宋体",1,18));
		name.setFont(new Font("宋体",1,18));
		showPost.setFont(new Font("宋体",1,18));
		showName.setFont(new Font("宋体",1,18));
		
		//添加监听
		exit.addActionListener(this);
		set.addActionListener(this);
		
		//设置布局
		this.setLayout(null);
		this.setBounds(700,300,400,280);
		set.setBounds(70,150,80,30);
		exit.setBounds(230,150,80,30);
		write.setBounds(90,50,200,25);
		name.setBounds(90,100,200,25);
		showPost.setBounds(30,47,150,30);
		showName.setBounds(36,97,120,30);
		
		//添加组件
		this.add(exit);
		this.add(set);
		this.add(write);
		this.add(name);
		this.add(showName);
		this.add(showPost);
		
		//窗体可见
		this.setVisible(true);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		JButton button = (JButton) e.getSource();
		if(button.equals(exit)){//退出
			System.exit(0);
		}else if(button.equals(set)){//确认
			String message = write.getText();
			int post;
			try{
				post = Integer.parseInt(message);
			}catch(NumberFormatException exception){
				JOptionPane.showMessageDialog(null, "请输入整数");
				write.setText("");
				return;
			}
			if("".equals(name.getText())){
				JOptionPane.showMessageDialog(null, "名称不能为空");
				return;
			}
			this.dispose();
			new ClientMainFrame(post,name.getText());
		}
	}
	
}
